package com.lnlyj.media.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;

/**
 * Create By Lnlyj on 2019/11/28
 **/
public abstract class BaseFragment extends Fragment implements IBaseInterface {

    private static final String TAG = "BaseFragment";
    
    private BaseActivity mActivity;
    private View mRootView;
    private int mLayoutId = 0;

    //是否是第一次开启网络加载
    private boolean isFirstLoadData = true;
    private boolean isUseLazy = false;

    public BaseFragment(int layoutId, boolean isUseLazy) {
        super(layoutId);
        this.mLayoutId = layoutId;
        this.isUseLazy = isUseLazy;
    }

    public BaseFragment() {
        super();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) context;
        initBundleData();
        initViewModel();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRootView = view;
        initView();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!isUseLazy) {
            initData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isUseLazy && isFirstLoadData) {
            isFirstLoadData = false;
            initData();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirstLoadData = true;
    }

    @Override
    public void showLoading() {
        mActivity.showLoading();
    }

    @Override
    public void hideLoading() {
        mActivity.hideLoading();
    }

    @Override
    public String getEditString(int resId) {
        View view = findViewById(resId);
        if (view instanceof TextView) {
            return ((TextView) view).getText().toString().trim();
        }
        return null;
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void setResult(int resultCode) {
        setResult(resultCode, null);
    }

    public void setResult(int resultCode, Intent data) {
        mActivity.setResult(resultCode, data);
        mActivity.finish();
    }

    public final <T extends View> T findViewById(@IdRes int resId) {
        return mRootView.findViewById(resId);
    }

    public Application getApplication() {
        if (mActivity != null) {
            return mActivity.getApplication();
        }
        return null;
    }

    public void snackMsg(String msg) {
        if (mActivity != null) {
            mActivity.snackMsg(msg);
        }
    }

    public void snackMsg(@StringRes int resId) {
        snackMsg(getString(resId));
    }

    public void postDelayed(Runnable runnable, int delayed) {
        mActivity.postDelayed(runnable, delayed);
    }

    public void removeCallback(Runnable runnable) {
        mActivity.removeCallback(runnable);
    }


    /**
     * 加载bundle携带数据
     */
    public abstract void initBundleData();

    /**
     * 加载ViewModel
     */
    protected abstract void initViewModel();

    /**
     * 初始化UI
     */
    public abstract void initView();

    /**
     * 加载数据
     */
    public abstract void initData();

}
